package com.itliang.web.controller.hardware;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itliang.annotation.Log;
import com.itliang.core.model.dto.equipment.DataTypeRangeQueryRequest;
import com.itliang.core.model.entity.equipment.DataTypeRange;
import com.itliang.core.result.BaseResponse;
import com.itliang.core.result.CodeEnum;
import com.itliang.core.result.ResultUtils;
import com.itliang.enums.BusinessType;
import com.itliang.exception.BusinessException;
import com.itliang.service.hardware.DataTypeRangeService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : liangzhuoyuan
 */
@Api(tags = "预警范围管理接口")
@RestController
@RequestMapping("/admin/hardware/range")
public class DataTypeRangeController {

    @Resource
    private DataTypeRangeService dataTypeRangeService;


    // region 增删改查

    /**
     * 创建预警范围数据
     *
     * @param dataTypeRange
     * @return
     */
    @Log(title = "预警范围管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public BaseResponse<Long> addDataTypeRange(@RequestBody DataTypeRange dataTypeRange) {
        dataTypeRangeService.save(dataTypeRange);
        return ResultUtils.success(dataTypeRange.getId(), "恭喜，添加操作成功！");
    }

    /**
     * 删除预警范围数据
     *
     * @param id 预警范围数据id
     * @return
     */
    @Log(title = "预警范围管理", businessType = BusinessType.DELETE)
    @GetMapping("/delete")
    public BaseResponse<Boolean> deleteDataTypeRange(int id) {
        boolean b = dataTypeRangeService.removeById(id);
        return ResultUtils.success(b, "恭喜，删除操作成功！");
    }

    /**
     * 批量删除预警范围数据
     *
     * @param idList id集合
     * @return
     */
    @Log(title = "预警范围管理", businessType = BusinessType.BATCHDELETE)
    @PostMapping("/delete/batch")
    public BaseResponse<Boolean> batchDeleteDataTypeRange(@RequestBody List<Integer> idList) {
        boolean b = dataTypeRangeService.removeBatchByIds(idList);
        return ResultUtils.success(b, "恭喜，批量删除操作成功！");
    }

    /**
     * 更新预警范围数据
     *
     * @param dataTypeRange 预警范围数据信息
     * @return
     */
    @Log(title = "预警范围管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public BaseResponse<Boolean> updateDataTypeRange(@RequestBody DataTypeRange dataTypeRange) {
        boolean result = dataTypeRangeService.updateById(dataTypeRange);
        return ResultUtils.success(result, "恭喜，修改操作成功！");
    }

    /**
     * 根据 id 获取预警范围数据
     *
     * @param id 预警范围数据id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<DataTypeRange> getDataTypeRangeById(Integer id) {
        if (id <= 0) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        DataTypeRange dataTypeRange = dataTypeRangeService.getById(id);
        return ResultUtils.success(dataTypeRange);
    }

    /**
     * 获取预警范围数据列表
     *
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<DataTypeRange>> listDataTypeRange() {
        List<DataTypeRange> list = dataTypeRangeService.list();
        return ResultUtils.success(list);
    }


    /**
     * 分页获取预警范围数据数据列表
     *
     * @param dataTypeRangeQueryRequest 条件参数
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<DataTypeRange>> listDataTypeRangeByPage(DataTypeRangeQueryRequest dataTypeRangeQueryRequest) {
        long current = dataTypeRangeQueryRequest.getCurrent();
        long size = dataTypeRangeQueryRequest.getPageSize();
        Page<DataTypeRange> page = dataTypeRangeService.page(new Page<>(current, size));
        return ResultUtils.success(page);
    }

    // endregion
}
